﻿namespace Leo.Common.RabbitMQ
{
    //[NullableContext(1)]
    //[Nullable(0)]
    internal static class Utils
    {
        public const string TraceIdName = "traceId";

        public static string CombineRetryExchangeOrQueueName(string exchangeName)
        {
            return exchangeName + "-retry";
        }
         
        public static string CombineDeadExchangeOrQueueName(string exchangeName)
        {
            return exchangeName + "-DLX";
        }
         
        public static ExchangeWorkMode GetDeadOrRetryExchangeType(string exchangeType)
        {
            ExchangeWorkMode workMode;
            if (!Enum.TryParse<ExchangeWorkMode>(exchangeType, true, out workMode))
            {
                throw new ArgumentException("exchangeType not defined");
            }
            return Utils.GetDeadOrRetryExchangeType(workMode);
        }
         
        public static ExchangeWorkMode GetDeadOrRetryExchangeType(ExchangeWorkMode workMode)
        {
            if (workMode != ExchangeWorkMode.Delayed)
            {
                return workMode;
            }
            return ExchangeWorkMode.Direct;
        }
         
        //[NullableContext(2)]
        public static bool IsRetryStrategyValid(this MessageRetryStrategy retryStrategy, out RabbitExchangeInfo retryExchange, out string retryRoutingKey)
        {
            if (retryStrategy == null)
            {
                retryExchange = null;
                retryRoutingKey = null;
                return false;
            }
            retryExchange = retryStrategy.GetRetryExchange(out retryRoutingKey);
            return retryExchange != null;
        }
         
        //[NullableContext(2)]
        public static bool IsDeadStrategyValid(this MessageRejectStrategy rejectStrategy, out RabbitExchangeInfo exchange)
        {
            exchange = null;
            if (rejectStrategy == null)
            {
                return false;
            }
            exchange = rejectStrategy.GetDeadExchange();
            return exchange != null;
        }
          
    }
}
